package ua.org.extjsbuilder.editors;

import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.presentation.IPresentationReconciler;
import org.eclipse.jface.text.presentation.PresentationReconciler;
import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;

public class JSConfiguration extends SourceViewerConfiguration {
	private ColorManager colorsManager;
	private JSGeneralScanner scanner;
	private JSKeywordsScanner keywordsScanner;
	
	public JSConfiguration(ColorManager colorsManager) {
		this.colorsManager = colorsManager;
	}

	public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
		return new String[] {
			IDocument.DEFAULT_CONTENT_TYPE,
			JSPartitionsScanner.JS_COMMENT};
	}

	protected JSGeneralScanner getGeneralScanner() {
		if (scanner == null) {
			scanner = new JSGeneralScanner(colorsManager);
			scanner.setDefaultReturnToken(
				new Token(
					new TextAttribute(
						colorsManager.getColor(ColorManager.COMMENT))));
		}
		return scanner;
	}
	
	protected JSKeywordsScanner getKeywordsScanner() {
		if (keywordsScanner == null) {
			keywordsScanner = new JSKeywordsScanner(colorsManager);
			keywordsScanner.setDefaultReturnToken(
				new Token(
					new TextAttribute(
						colorsManager.getColor(ColorManager.DEFAULT))));
		}
		return keywordsScanner;
	}

	public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
		PresentationReconciler reconciler = new PresentationReconciler();

		DefaultDamagerRepairer dr =
			new DefaultDamagerRepairer(getGeneralScanner());
		reconciler.setDamager(dr, JSPartitionsScanner.JS_COMMENT);
		reconciler.setRepairer(dr, JSPartitionsScanner.JS_COMMENT);

		dr = new DefaultDamagerRepairer(getKeywordsScanner());
		reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
		reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);

		/*NonRuleBasedDamagerRepairer ndr =
			new NonRuleBasedDamagerRepairer(
				new TextAttribute(
					colorManager.getColor(IXMLColorConstants.XML_COMMENT)));
		reconciler.setDamager(ndr, XMLPartitionScanner.XML_COMMENT);
		reconciler.setRepairer(ndr, XMLPartitionScanner.XML_COMMENT);*/

		return reconciler;
	}
}
